var appModule = angular.module('appModule', []);
var preventImgEffect;
var moveFloatPointCont;

//整体页面控制器
appModule.controller("appController", ["$scope", "$http","$filter","$sce", function ($scope, $http,$timeout,$filter,$sce) {
	var initMui;
	var pullInfoObj;
	var receivedData;

	$scope.init = function () {
		$scope.selectedIndex=0;
		$scope.showFlag=false;
		$scope.tags=["常用功能","办公类","事务类","查询类","其他类"];
		$scope.direcotories=["office","affair","search","others"];
		var str="";
		//获取常用功能
		$.ajax({
            type: "get",
            url: window.ViewContextPath + "system/layui/loadModelsByUsercode",
            async: false,
            dataType: "json",
            success: function (data) {
                str =data.data;
            }
        });

		var itemsArr=$scope.itemsArr=[
            str,
			[{"name":"公文管理","code":"doctrans","modelclass":"","imgurl":"modules/dashboard/image/gwgl","imgType":".png"},
				{"name":"审批服务","code":"SPFW","modelclass":"","imgurl":"modules/dashboard/image/spfw","imgType":".png"},
				{"name":"人事考核","code":"PERSONNEL","modelclass":"","imgurl":"modules/dashboard/image/rskh","imgType":".png"},
				{"name":"人事管理","code":"PERSONNEL","modelclass":"rsgl","imgurl":"modules/dashboard/image/rsgl","imgType":".png"},
                {"name":"档案管理","code":"ARCHIVE","modelclass":"","imgurl":"modules/dashboard/image/dagl","imgType":".png"},
				{"name":"资产管理","code":"OA","modelclass":"zcgl","imgurl":"modules/dashboard/image/zcgl","imgType":".png"},
				{"name":"项目管理","code":"XMGL","modelclass":"","imgurl":"modules/dashboard/image/xmgl","imgType":".png"},
				{"name":"稽察管理","code":"random","modelclass":"","imgurl":"modules/dashboard/image/jcgl","imgType":".png"}],
			[{"name":"会议管理","code":"MEET","modelclass":"","imgurl":"modules/dashboard/image/hygl","imgType":".png"},
                {"name":"用户委托","code":"OPTMNG","modelclass":"RWWT","imgurl":"modules/dashboard/image/yhwt","imgType":".png"},
				{"name":"差假管理","code":"OA","modelclass":"QJGL","imgurl":"modules/dashboard/image/cjgl","imgType":".png"},
                {"name":"值班管理","code":"OA","modelclass":"ZBGL","imgurl":"modules/dashboard/image/zbgl","imgType":".png"},
				{"name":"日程管理","code":"OA","modelclass":"RCGL","imgurl":"modules/dashboard/image/rcgl","imgType":".png"},
				{"name":"考试培训","code":"PERSONNEL","modelclass":"pxgl","imgurl":"modules/dashboard/image/kspx","imgType":".png"},
				{"name":"党建管理","code":"PARTYBUILD","modelclass":"","imgurl":"modules/dashboard/image/djgl","imgType":".png"}],
			[{"name":"收文查询","code":"doctrans","modelclass":"cxswcx","imgurl":"modules/dashboard/image/hycx","imgType":".png"},
				{"name":"发文查询","code":"doctrans","modelclass":"cxfwcx","imgurl":"modules/dashboard/image/bjcx","imgType":".png"},
				{"name":"通讯录","code":"CONTACTS","modelclass":"","imgurl":"modules/dashboard/image/txl","imgType":".png"}],
			[
				/*{"name":"工作流","code":"apprFlow","modelclass":"","imgurl":"modules/dashboard/image/grsz","imgType":".png"},*/
				{"name":"领导日程","code":"OA","modelclass":"LEADERSCHEDULE","imgurl":"modules/dashboard/image/ldrc","imgType":".png"},
				{"name":"统计报表","code":"tjbb","modelclass":"","imgurl":"modules/dashboard/image/tjbb","imgType":".png"},
				{"name":"后台管理","code":"centitIp","modelclass":"","imgurl":"modules/dashboard/image/htgl","imgType":".png"},
				{"name":"数据交互","code":"sjjh","modelclass":"","imgurl":"modules/dashboard/image/sjjh","imgType":".png"},
                {"name":"数据公开","code":"ssjgk","modelclass":"","imgurl":"modules/dashboard/image/ssjgk","imgType":".png"},
                {"name":"政策法规","code":"zcfg","modelclass":"","imgurl":"modules/dashboard/image/zcfg","imgType":".png"},
                {"name":"多媒体库","code":"dmtk","modelclass":"","imgurl":"modules/dashboard/image/dmtk","imgType":".png"},
                {"name":"营商环境工作平台","code":"yshj","modelclass":"","imgurl":"modules/dashboard/image/yshj","imgType":".png"},
                {"name":"网站登陆","code":"wzdl","modelclass":"","imgurl":"modules/dashboard/image/wzdl","imgType":".png"}
				]
		];
		var countPerRow=4;
		for(var i=0;i<itemsArr.length;i++){
			var curItems=itemsArr[i];
			var curRowCount=Math.ceil(curItems.length/countPerRow);
			for(var j=0,tempArr=[];j<curRowCount;j++){
				var curRowInfo=curItems.slice(j*countPerRow,(j+1)*countPerRow);
				var lenOfCurRowInfo=curRowInfo.length;
				if(lenOfCurRowInfo<countPerRow){
					for(var k=0;k<countPerRow-lenOfCurRowInfo;k++){
						curRowInfo.push({hide:true});
					}
				}
				tempArr.push(curRowInfo);
			}
			itemsArr[i]=tempArr;
		}
		
		setTimeout(function(){
			//1、标签样式调整
			var tagJq=$(".float-point .tag");
			var tagsLen=tagJq.length;
			var tagsWidth=tagJq.parent().width();
			var sumOfTagWidth=0;
			tagJq.each(function(){
				sumOfTagWidth+=$(this).outerWidth();
			});
			tagJq.css("marginLeft",parseFloat((tagsWidth-sumOfTagWidth)/(tagsLen+1))+"px");

			//2、条目所在行样式调整
			$(".float-point .items-row").each(function(){
				var itemJq=$(this).children(".item");
				var itemsLen=itemJq.length;
				var itemRowWidth=$(this).width();
				var sumOfItemWidth=0;
				itemJq.each(function(){
					sumOfItemWidth+=$(this).outerWidth();
				});
				itemJq.css("marginLeft",parseFloat((itemRowWidth-sumOfItemWidth)/(itemsLen+1))+"px");
			});
		});
    };

	$scope.judgeDisabled=function(str,item){
		if(typeof(item.modelclass)!="undefined"&& item.modelclass!=""&&str.indexOf(item.modelclass)==-1){
			item.disabled=true;
			return true;
		}
		if(str.indexOf(item.code)==-1&&item.modelclass==""){
            item.disabled=true;
            return true;
		}
		return false;
	};

	$scope.return=function(){
		window.ideainfo.goback();
	};

	$scope.switchTag=function(index){
		if($scope.selectedIndex==index){return;}
		$scope.selectedIndex=index;
		var itemsPageWitdh=$(".items-page").width();
		$(".items-page-cont").css("transform","translateX(-"+itemsPageWitdh*index+"px)");
	};
	
	$scope.handleItemClick=function(item){
		if(item.disabled){return;}
		item.selected=true;
		setTimeout(function(){
			item.selected=false;
			$scope.$apply();
		},300);
        optIndex(item.code,item.modelclass);
	};

	$(function(){
		var startX;
		var startY;
		var endX;
		var endY;
		var dragFlag;
		$(".float-point").myDrag({
			parent:'parent',
			randomPosition:false,
			dragStart:function(x,y){
				startX=x;
				startY=y;
				dragFlag=false;
			},
			dragMove:function(x,y){
				dragFlag=true;
				var pointSize=$(".float-point").width();
				var boxWidth=$("body").width();
				var boxHeight=$("body").height();
				if(x<boxWidth/2-pointSize/2){
					$(".oper-region").removeClass("right").addClass("left");
				}else{
					$(".oper-region").removeClass("left").addClass("right");
				}
				if(y<boxHeight/2-pointSize/2){
					$(".oper-region").removeClass("down").addClass("up");
				}else{
					$(".oper-region").removeClass("up").addClass("down");
				}
			},
			dragEnd:function(x,y){
				endX=x;
				endY=y;
				if(!dragFlag&&startX==endX&&startY==endY){
					$scope.showFlag=!$scope.showFlag;
					$scope.$apply();
				}
			}
		});
		
		$(".gray-mask").click(function(){
			$scope.showFlag=false;
			$scope.$apply();
		});
	});
}]);
